package com.lideru.backstage.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lideru.backstage.entity.Book;
import com.lideru.backstage.entity.Message;
import com.lideru.backstage.mapper.BookMapper;
import com.lideru.backstage.mapper.MessageMapper;
import com.lideru.backstage.utils.JWTUtils;
import com.lideru.backstage.utils.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * @author lideru
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    MessageMapper messageMapper;
    @Resource
    private BookMapper bookMapper;

    @PostMapping
    public Result<?> save(@RequestBody Message message, HttpServletRequest request) {
        Book book = bookMapper.selectById(message.getBookId());
        if (book == null) {
            return Result.error("-2","书本不存在！");
        }
        message.setBookName(book.getName());
        message.setGmtCreate(new Date());
//        message.setGmtModified(new Date());
        message.setUserId(JWTUtils.getUserId(request.getHeader("token")));
        int insert = messageMapper.insert(message);
        return Result.success("留言成功");
    }

    @PutMapping
    public Result<?> update(@RequestBody Message Message) {
        Message.setGmtModified(new Date());
        int i = messageMapper.updateById(Message);
        if (i < 1) {
            return Result.error("-1", "修改失败！");
        }
        return Result.success();
    }
    @PutMapping("reply")
    public Result<?> reply(@RequestBody Message message) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", message.getId());
        updateWrapper.eq("book_id", message.getBookId());
        updateWrapper.set("reply",message.getReply() );
        updateWrapper.set("gmt_modified",new Date());
        int i = messageMapper.update(null,updateWrapper);
        if (i < 1) {
            return Result.error("-1", "修改失败！");
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        Message Message1 = messageMapper.selectOne(Wrappers.<Message>lambdaQuery()
                .eq(Message::getId, id));
        if (Message1 != null) {
            messageMapper.deleteById(id);
            return Result.success();
        }
        return Result.error("403", "你没有权限");

    }

    @GetMapping("getAllMessage")
    public Result<?> getAllMessage() {
        return Result.success(messageMapper.selectList(null));
    }

    @GetMapping("getUserAllMessage")
    public Result<?> getUserAllMessage(@RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10") Integer pageSize,
                                              @RequestParam(defaultValue = "") String search, HttpServletRequest request) {
        return Result.success(
                messageMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<Message>lambdaQuery().
                        eq(Message::getUserId, JWTUtils.getUserId(request.getHeader("token")))
                        .last("order by gmt_modified  desc")));
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search, HttpServletRequest request) {
        Page<Message> page = messageMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<Message>lambdaQuery().
                eq(Message::getUserId, JWTUtils.getUserId(request.getHeader("token")))
                .eq(Message::getUserId, JWTUtils.getUserId(request.getHeader("token")))
                .like(Message::getReply, search));

        return Result.success(page);
    }
    @GetMapping("getUserMessage")
    public Result<?> getUserMessage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search, HttpServletRequest request) {
        Page<Message> list = messageMapper.getDataListBySql(new Page<>(pageNum, pageSize), search ,JWTUtils.getUserId(request.getHeader("token")));
        return Result.success(list);
    }

    /**
     * 统计数据
     *
     * @return
     */
    @GetMapping("/count")
    public Result<?> count(HttpServletRequest request) {
//        User user = getUser(); // 当前登录的用户信息
        return Result.success(messageMapper.countMessage( JWTUtils.getUserId(request.getHeader("token"))));
    }

}
